﻿/*
	Copyright 2010 Intuitive Solutions

	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at

	   http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/

using System;
using System.IO;
using System.Xml;
using Metamorph.Core.Bpm;
using Metamorph.Core.Smm;
using Metamorph.Configuration;
using Metamorph.Core.Bpm.Events;
using System.Collections.Generic;
using Metamorph.Core.Bpm.Activities;

namespace Metamorph.Core
{
	public class StateMachineProvider
	{
		private Dictionary<string, State>			dStates;
		private StateMachineDefinition				smStateMachine;
		private XmlTextReader						xtrXmlTextReader;
		private string								sStateMachineName;

		public StateMachineProvider()
		{
		}

		public StateMachineDefinition StateMachine
		{
			get
			{
				return smStateMachine;
			}
		}

		public void Load(string sStateMachineName)
		{
			FileStream srFile;
            string sFilePath = MetamorphConfiguration.Instance.StateMachineDefinition;

			this.sStateMachineName = sStateMachineName;

			using (srFile = new FileStream(sFilePath, System.IO.FileMode.Open, FileAccess.Read))
			{
				xtrXmlTextReader = new XmlTextReader(srFile);

				xtrXmlTextReader.XmlResolver = null;

				xtrXmlTextReader.Read();
				xtrXmlTextReader.MoveToContent();

				TraverseXml();

				xtrXmlTextReader.Read();
				xtrXmlTextReader.MoveToContent();
			}
		}

		public void TraverseXml()
		{
			if ("metamorph".Equals(xtrXmlTextReader.Name.ToLower()))
			{
				IgnoreStateMachineDefinition();

				LoadStateMachine();
			}
		}

		public void LoadStateMachine()
		{
			bool bIgnoreStateMachine = false;

			if ("statemachine".Equals(xtrXmlTextReader.Name.ToLower()))
			{
				while ("statemachine".Equals(xtrXmlTextReader.Name.ToLower()) &&
						smStateMachine == null)
				{
					for (int i = 0; i < xtrXmlTextReader.AttributeCount; i++)
					{
						xtrXmlTextReader.MoveToNextAttribute();

						if ("name".Equals(xtrXmlTextReader.Name))
						{
							if (!xtrXmlTextReader.Value.Equals(sStateMachineName))
							{
								bIgnoreStateMachine = true;

								break;
							}
							else
							{
								dStates				= new Dictionary<string, State>();
								smStateMachine		= new StateMachineDefinition(sStateMachineName);

								smStateMachine.States = dStates;
							}
						}
						else if ("initialstate".Equals(xtrXmlTextReader.Name.ToLower()))
						{
							if (xtrXmlTextReader.Value.Length > 0)
							{
								smStateMachine.InitialState = GetState(xtrXmlTextReader.Value);
							}
						}

						xtrXmlTextReader.ReadAttributeValue();
					}

					xtrXmlTextReader.Read();
					xtrXmlTextReader.MoveToContent();

					if (bIgnoreStateMachine)
					{
						IgnoreStateMachineDefinition();

						bIgnoreStateMachine = false;
					}
					else
					{
						LoadStates();
					}
				}
			}
		}

		private void IgnoreStateMachineDefinition()
		{
			while (!("statemachine".Equals(xtrXmlTextReader.Name.ToLower()) &&
				xtrXmlTextReader.IsStartElement()) &&
				!xtrXmlTextReader.EOF)
			{
				xtrXmlTextReader.Read();
				xtrXmlTextReader.MoveToElement();
			}
		}

		private void LoadStates()
		{
			if ("state".Equals(xtrXmlTextReader.Name.ToLower()))
			{
				while ("state".Equals(xtrXmlTextReader.Name.ToLower()))
				{
					State sState = null;

					for (int i = 0; i < xtrXmlTextReader.AttributeCount; i++)
					{
						xtrXmlTextReader.MoveToNextAttribute();

						if ("name".Equals(xtrXmlTextReader.Name))
						{
							sState = GetState(xtrXmlTextReader.Value);
						}

						xtrXmlTextReader.ReadAttributeValue();
					}

					xtrXmlTextReader.Read();
					xtrXmlTextReader.MoveToContent();

					LoadStateTransitions(sState);
				}

                xtrXmlTextReader.Read();
                xtrXmlTextReader.MoveToContent();
			}
		}

		private void LoadStateTransitions(State sState)
		{
			if ("transition".Equals(xtrXmlTextReader.Name.ToLower()))
			{
				while ("transition".Equals(xtrXmlTextReader.Name.ToLower()))
				{
					Transition tTransition = null;

					for (int i = 0; i < xtrXmlTextReader.AttributeCount; i++)
					{
						xtrXmlTextReader.MoveToNextAttribute();

						if ("name".Equals(xtrXmlTextReader.Name))
						{
							tTransition = new Transition(xtrXmlTextReader.Value);
						}
						else if ("nextstate".Equals(xtrXmlTextReader.Name.ToLower()))
						{
							tTransition.Target = GetState(xtrXmlTextReader.Value);
						}
						else if ("action".Equals(xtrXmlTextReader.Name))
						{
							if (xtrXmlTextReader.Value.Length > 0)
							{
								tTransition.ActionName = xtrXmlTextReader.Value;
							}
						}
						else if ("condition".Equals(xtrXmlTextReader.Name))
						{
							if (xtrXmlTextReader.Value.Length > 0)
							{
								tTransition.ConditionName = xtrXmlTextReader.Value;
							}
						}

						xtrXmlTextReader.ReadAttributeValue();
					}

					tTransition.Source = sState;

					sState.Transitions.Add(tTransition.Name, tTransition);

					xtrXmlTextReader.Read();
					xtrXmlTextReader.MoveToContent();
				}

                xtrXmlTextReader.Read();
                xtrXmlTextReader.MoveToContent();
			}
		}

		public State GetState(string sStateName)
		{
			State sState = null;

			if (dStates.ContainsKey(sStateName))
			{
				sState = dStates[sStateName];
			}
			else
			{
				sState = new State(sStateName);

				dStates.Add(sStateName, sState);
			}

			return sState;
		}
	}
}
